﻿using System.Text;

namespace Sharp.Portal.AdminLTE.FormField
{
    /// <summary>
    /// 图片上传字段
    /// </summary>
    public class ImageUploadField : ElementField
    {
        /// <summary>
        /// 图片上传字段
        /// </summary>
        /// <param name="fieldName"></param>
        public ImageUploadField(string fieldName)
        {
            this.FieldName = fieldName;
        }
        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="nonFirst"></param>
        /// <returns></returns>
        public override string Build(bool nonFirst = false)
        {
            if (this.IsBrowsing() && !this.KeepingXType)
            {
                return new ImageField(this.FieldName).Build(nonFirst);
            }
            var sb = new StringBuilder();
            sb.AppendFormat("<img id=\"thumbnail{0}\" width=\"160\" alt=\"\" class=\"thumbnail img-responsive\" />", FieldName);


            sb.AppendLine();
            sb.AppendFormat("@Html.Bootstrap().TextBox().Id(\"{0}FileUpload\")", FieldName);
            sb.Append(".Type().File().Attribute(\"accept\",\"image/*\")");

            sb.AppendLine();
            sb.AppendFormat("<input id=\"{0}\" name=\"{0}\" type=\"hidden\" value=\"\" />", FieldName);
            sb.AppendLine();

            sb.AppendFormat("<script type = \"text/javascript\">\r\nvar {1}List = new app.ImageUpload();\r\n{1}List.init(\"{1}\", \"@Model.{0}\");\r\n</script>", FieldName, FieldName.Replace(".", "_"));


            sb.AppendFormat("@Html.ValidationMessageFor(m => m.{0})", FieldName);
            return sb.ToString();
        }
    }
}
